# 自定义分页器 局部
from django_filters.rest_framework import DjangoFilterBackend
from itsdangerous import Serializer
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import BasePermission
from rest_framework.response import Response
from rest_framework.views import APIView

from course.models import Course, Category, Label, Section, Jie, MyCourse, Attention
from course.serializers import CourseSerializers, MyCourseSerializers, CategorySerializers, LabelSerializers, \
    SectionSerializers, JieSerializers


class PageNum(PageNumberPagination):
    # 查询字符串中代表每页返回数据数量的参数名, 默认值: None
    page_size_query_param = 'page_size'
    # 查询字符串中代表页码的参数名, 有默认值: page
    # page_query_param = 'page'
    # 一页中最多的结果条数
    max_page_size = 2

class CourseViewSet(viewsets.ModelViewSet):
    """
    完成产品的增删改查
    """
    queryset = Course.objects.all()
    serializer_class = CourseSerializers
    Serializer  # 优先使用 get_serializer_class 返回的序列化器
    # 指定过滤方法类, 排序方法类, 一个或多个
    filter_backends = (DjangoFilterBackend, OrderingFilter)  # 同时支持过滤和排序
    # 指定排序字段, 不设置, 排序功能不起效
    ordering_fields = ('create_time', 'id', 'update_time', 'attenttion_num')
    # 指定过滤字段, 不设置, 过滤功能不起效
    # filter_fields = ('curser', '', 'is_active')
    # 自定义分页器 覆盖全局配置
    pagination_class = PageNum



    # @action(methods=['get'], detail=False)
    # def unactived(self, request, *args, **kwargs):
    #     # 获取查询集, 过滤出未激活的用户
    #     qs = self.queryset.filter(is_active=False)
    #     # 使用序列化器, 序列化查询集, 并且是
    #     ser = self.get_serializer(qs)
    #
    #     return Response(ser.data)

class CategoryViewSet(viewsets.ModelViewSet):
    """
    完成类别表   (免费, 限免, 会员)的增删改查
    """
    queryset = Category.objects.all()
    serializer_class = CategorySerializers

class LabelViewSet(viewsets.ModelViewSet):
    """
    完成标签表的增删改查
    """
    queryset = Label.objects.all()
    serializer_class = LabelSerializers
class SectionViewSet(viewsets.ModelViewSet):
    """
    完成章表的增删改查
    """
    queryset = Section.objects.all()
    serializer_class = SectionSerializers

class JieViewSet(viewsets.ModelViewSet):
    """
    完成节表的增删改查
    """
    queryset = Jie.objects.all()
    serializer_class = JieSerializers


class MyCourseViewSet(viewsets.ModelViewSet):
    """
    完成我的课程的增删改查
    """
    queryset = MyCourse.objects.all()
    serializer_class = MyCourseSerializers


# 关注
class AttentionView(APIView):
    def post(self, request):
        user_id = request.data['user_id']
        data = Attention.objects.filter(user_id=user_id).first()
        try:
            if data.status == 0:
                data.status = 1
                data.number += 1
                data.save()
                return Response({"code": 200, "message": "点赞成功", "status": data.status, "data": data.number})
            elif data.status == 1:
                data.status = 0
                data.number -= 1
                data.save()
                return Response({"code": 200, "message": "取消点赞成功", "status": data.status, "data": data.number})
        except Exception as e:
            return Response({"code": 400, "message": "失败"})